package gameEngine.com.Actor;

import java.util.Random;

import android.util.Log;
import gameEngine.com.GameLayer;
import gameEngine.com.GameLib;
import gameEngine.com.IConstant;
import gameEngine.com.MainActivity;
import gameEngine.com.Sprite;
import gameEngine.com.state.StateGameplay;
import gameEngine.com.state.StateLoading;

public class TankEnemy extends Actor implements IConstant {
	public String levelString = " ";
	

	public static Sprite publicSprite;
	public int  angleDegree = 0;
	public double  angleRadian = 0;	
	//public static Sprite commonSprite ; 
	public int directMove_x = 0;
	public int directMove_y = 0;
	public static final int STATE_IDE = 0;
	public static final int STATE_AUTO_MOVE = 1;
	public static final int STATE_TRACK_MC_MOVE = 2;
	public static final int STATE_ATTACK_MC = 3;
	public static final int STATE_HURT_MC = 4;
	public static final int STATE_DIE = 5;

	public int state = -1;

	public TankEnemy() {
		// TODO Auto-generated constructor stub
		super();
	}

	public TankEnemy(int x, int y, int w, int h, String name, String type) {
		// TODO Auto-generated constructor stub
		super(x, y, w, h, name, type);
	}

	public void render() {
		
		//drawAnim_(Canvas g, int angle,int Scalex,int Scaley,int _currentAnimation,int _currentFrame, int _waitDelay, int x, int y)
		//drawAnim(MainActivity.mainCanvas,0,1,1, _currentAnimation,_currentFrame,_waitDelay,m_x + GameLayer.screenOffsetX, m_y + GameLayer.screenOffsetY);
		publicSprite.drawAnim(MainActivity.mainCanvas,this._currentAnimation,1,1, this,m_x + GameLayer.screenOffsetX, m_y + GameLayer.screenOffsetY);
		//Log.d("   " , " "+( m_x + GameLayer.screenOffsetX));
		//	GameLib.mainCanvas.drawRect((m_x + GameLayer.screenOffsetX), (m_y + GameLayer.screenOffsetY), (m_x + GameLayer.screenOffsetX) + m_Width, (m_y + GameLayer.screenOffsetY) + m_Height, GameLib.mainPaint);
		MainActivity.fontsmall.drawString(GameLib.mainCanvas, " " +angleDegree, (m_x + GameLayer.screenOffsetX) -10, (m_y + GameLayer.screenOffsetY)-15, 0);
		drawRect(m_x + GameLayer.screenOffsetX,m_y+ GameLayer.screenOffsetY,m_Width ,m_Height );
	}

	

	public void updateAutoMove() {
	
		if (directMove_x <= 0 && directMove_y <= 0) {
			directMove_x = random.nextInt() % m_speedx;
			directMove_y = random.nextInt() % m_speedy;
		}
		if (!GameLayer.checkCollisionPhysic(GameLayer.arrayListDataMap.get(GameLayer.arrayListDataMap.size() - 1), m_x + directMove_x, m_y + directMove_y, 32, 32))
		{
			if ((m_x + directMove_x + GameLayer.screenOffsetX < -50) || ((m_x + GameLayer.screenOffsetX + directMove_x) > GameLib.SCREEN_WIDTH)
					|| (m_y + directMove_y + GameLayer.screenOffsetY < -50) || ((m_y + directMove_y + GameLayer.screenOffsetY) > GameLib.SCREEN_HEIGHT))
			{
				directMove_x = random.nextInt() % m_speedx;
				directMove_y = random.nextInt() % m_speedy;
			} else
			{
				m_x += directMove_x;
				m_y += directMove_y;
			}
		}
		else //set new target		
		{
			directMove_x = random.nextInt() % m_speedx;
			directMove_y = random.nextInt() % m_speedy;

		}

	}
	public void updateAutoFireBullet() {
		
		if((MainActivity.frameCountCurrentState - frameBulletCount > bullteFireRate)  && StateGameplay.tankMC.state !=  TankMC.STATE_DIE)
		{
		//	StateGameplay.actorBulletList.add(Bullet.creaBullet(this,GameLib.touchPosX,GameLib.touchPosY, false,true));
			StateGameplay.listBullet.add(Bullet.creaBullet(this, StateGameplay.tankMC.m_x + GameLayer.screenOffsetX,StateGameplay.tankMC.m_y+ GameLayer.screenOffsetY,true,false));
			frameBulletCount = MainActivity.frameCountCurrentState;
		}
	}
	public void update() {
		switch (state) {
		case STATE_IDE:

			break;
		case STATE_AUTO_MOVE:
			updateAutoMove() ;
			updateAutoFireBullet();
			break;
		case STATE_TRACK_MC_MOVE: 

			break;
		case STATE_ATTACK_MC:

			break;
		case STATE_HURT_MC:

			break;
		case STATE_DIE:
			if(publicSprite.hasAnimationFinished(_currentAnimation,_currentFrame,_waitDelay)){
				state = -1;
			}
			break;
		default:
			break;
		}
		if (checkCollision(this, StateGameplay.tankMC)) {
			attack(StateGameplay.tankMC);
		}

	}

	public void attack(Actor actor) {
		// TODO Auto-generated method stub

	}
}
